package com.sunmnet.j2ee.service.powerdb.designer.impl;

import com.sunmnet.j2ee.core.powerdb.designer.Commons;
import com.sunmnet.j2ee.core.pub.sqlloader.SqlLoader;
import com.sunmnet.j2ee.dao.BaseDao;
import com.sunmnet.j2ee.entity.powerdb.designer.Field;
import com.sunmnet.j2ee.entity.powerdb.designer.IndexInfo;
import com.sunmnet.j2ee.service.impl.BaseServiceImpl;
import com.sunmnet.j2ee.service.powerdb.designer.IndexInfoService;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.sql.Timestamp;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service("/powerdb/designer/indexInfoService")
public class IndexInfoServiceImpl extends BaseServiceImpl<IndexInfo, Long> implements IndexInfoService {

    @Override
    @Resource(name = "/powerdb/designer/indexInfoDao")
    public void setDao(BaseDao<IndexInfo, Long> dao) {
        super.dao = dao;
    }

    /**
     * 日志
     */
    private static final Logger LOG = LogManager.getLogger(IndexInfoServiceImpl.class.getName());

    private static final String CLASS_NAME = IndexInfoServiceImpl.class.getName();

    private static final String SQL_DELETE_INDEX_BY_FIELDID =
            SqlLoader.getInstance().getSql(CLASS_NAME, "SQL_DELETE_INDEX_BY_FIELDID");

    @Override
    public List<IndexInfo> getIndexs(Long entityId, Long versionId) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("entityId", entityId);
        params.put("versionId", versionId);
        String hql = "FROM IndexInfo i WHERE i.entityId = :entityId AND i.versionId = :versionId";
        return dao.find(hql, params);
    }

    @Override
    public List<IndexInfo> getIndexInfos(Long moduleId, Long versionId) {
        String hql = "FROM IndexInfo WHERE moduleId = " + moduleId + " AND versionId = " + versionId;
        return dao.findByHql(hql);
    }

    @Override
    public void copyIndexs(List<IndexInfo> list, Map<Long, Long> copyedEntitys, Map<Long, Long> copyedFields, Long versionId, Long newModuleId) {
        LOG.debug("copyIndexs: {},{},{},{},{}", list, copyedEntitys, copyedFields, versionId, newModuleId);

        IndexInfo tmp = null;
        for (IndexInfo in : list) {
            tmp = clone(in, versionId, newModuleId, copyedEntitys.get(in.getEntityId()), copyedFields.get(in.getFieldId()));
            dao.save(tmp);
        }
    }

    private IndexInfo clone(IndexInfo in, Long versionId, Long newModuleId, Long newEntityId, Long newFieldId) {
        IndexInfo indexInfo = new IndexInfo();

        indexInfo.setEntityId(newEntityId);
        indexInfo.setModuleId(newModuleId);
        indexInfo.setFieldId(newFieldId);
        indexInfo.setVersionId(versionId);
        indexInfo.setName(in.getName());
        indexInfo.setType(in.getType());
        indexInfo.setCreateTime(in.getCreateTime());
        indexInfo.setModifyTime(in.getModifyTime());
        indexInfo.setScript(in.getScript());
        indexInfo.setCreateAccountId(in.getCreateAccountId());
        indexInfo.setModifyAccountId(in.getModifyAccountId());
        indexInfo.setNameCn(in.getNameCn());
        return indexInfo;
    }

    @Override
    public void addPmKeyFieldToIndex(Field obj) {

        IndexInfo index = new IndexInfo();
        index.setCreateTime(new Timestamp((new Date()).getTime()));
        index.setModifyTime(new Timestamp((new Date()).getTime()));
        index.setName(obj.getName() + '_' + obj.getId());
        index.setNameCn(obj.getNameCn() + '_' + obj.getId());
        index.setType((short) 1);
        index.setFieldId(obj.getId());
        index.setScript(obj.getScript());
        index.setEntityId(obj.getEntityId());
        index.setCreateAccountId(obj.getCreateAccountId());
        index.setModifyAccountId(obj.getModifyAccountId());
        index.setVersionId(Commons.DEF_VERSION_ID);
        index.setModuleId(obj.getModuleId());

        try {
            deleteIndexByFieldId(obj.getId());
            dao.save(index);
        } catch (Exception e) {
            LOG.error("主键字段添加索引失败" + e.getMessage(), e);
        }
    }

    @Override
    public short deleteIndexByFieldId(long fieldId) throws Exception {
        Map<String, Object> params = new HashMap<>();
        params.put("fieldId", fieldId);
        return (short) dao.executeSql(SQL_DELETE_INDEX_BY_FIELDID, params);
    }
}
